import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QPushButton,
    QFileDialog,
    QLineEdit,
    QTextEdit,
    QProgressBar,
)
from PyQt5.QtCore import QObject, QThread, pyqtSignal, Qt
from scapy.all import *
import time
import os

from scapy.layers.inet import IP, TCP
from scapy.layers.l2 import Ether

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        self.read_pcap_thread = None

    def init_ui(self):
        self.setGeometry(200, 200, 600, 500)
        self.setWindowTitle("Pcap Replay")

        # 添加选择 pcap 文件的按钮
        self.select_file_btn = QPushButton("Select Pcap File", self)
        self.select_file_btn.move(20, 20)
        self.select_file_btn.clicked.connect(self.select_file)

        # 添加用于显示回放数据的 QTextEdit
        self.text_edit = QTextEdit(self)
        self.text_edit.move(20, 120)
        self.text_edit.resize(550, 200)

        # 添加输入回放地址的 QLineEdit
        self.replay_address_edit = QLineEdit(self)
        self.replay_address_edit.move(20, 80)

        # 添加开始回放、暂停回放和结束回放的按钮
        self.start_replay_btn = QPushButton("Start", self)
        self.start_replay_btn.move(20, 340)
        self.start_replay_btn.clicked.connect(self.start_replay)

        self.pause_replay_btn = QPushButton("Pause", self)
        self.pause_replay_btn.move(100, 340)
        self.pause_replay_btn.clicked.connect(self.pause_replay)

        self.stop_replay_btn = QPushButton("Stop", self)
        self.stop_replay_btn.move(180, 340)
        self.stop_replay_btn.clicked.connect(self.stop_replay)

        # 添加 PACP 文件加载进度条和回放进度条
        self.pcap_progressbar = QProgressBar(self)
        self.pcap_progressbar.setGeometry(20, 430, 550, 20)
        self.pcap_progressbar.setValue(0)

        self.replay_progressbar = QProgressBar(self)
        self.replay_progressbar.setGeometry(20, 460, 550, 20)
        self.replay_progressbar.setValue(0)

        self.show()

    def select_file(self):
        file_path, _ = QFileDialog.getOpenFileName(
            self, "Open file", ".", "Pcap Files (*.pcap)"
        )
        if file_path:
            self.file_path = file_path

    def start_replay(self):
        replay_address = self.replay_address_edit.text().strip()
        if not replay_address:
            return

        if not hasattr(self, "file_path"):
            return

        # 启动读取 pcap 文件的线程
        self.read_pcap_thread = ReadPcapThread(self.file_path, replay_address)
        self.read_pcap_thread.update_text.connect(
            self.update_text
        )  # 将更新 UI 的信号连接到 update_text 方法
        self.read_pcap_thread.update_pcap_progressbar.connect(
            self.update_pcap_progressbar
        )  # 连接加载 PACP 文件进度条的信号
        self.read_pcap_thread.update_replay_progressbar.connect(
            self.update_replay_progressbar
        )  # 连接回放进度条的信号
        self.read_pcap_thread.start()

        # 修改系统时间为 pcap 文件开始时间
        start_time_str = time.strftime("%Y%m%d%H%M.%S", time.localtime(start_time))
        os.system("date %s" % start_time_str)

    def pause_replay(self):
        # 调用线程的暂停方法
        if self.read_pcap_thread:
            self.read_pcap_thread.terminate()

    def stop_replay(self):
        # 调用线程的停止方法，重置系统时间
        if self.read_pcap_thread:
            self.read_pcap_thread.terminate()
            os.system("date %s" % time.strftime("%Y%m%d%H%M.%S"))

    def update_text(self, text):
        # 在 QTextEdit 中显示回放数据
        self.text_edit.insertPlainText(text)

    def update_pcap_progressbar(self, value):
        # 加载 PACP 文件进度条的值更新
        self.pcap_progressbar.setValue(value)

    def update_replay_progressbar(self, value):
        # 回放进度条的值更新
        self.replay_progressbar.setValue(value)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    sys.exit(app.exec_())
